package com.wn.pettyloan.controller;

import com.wn.pettyloan.dto.ProductDto;
import com.wn.pettyloan.entity.Product;
import com.wn.pettyloan.service.ProductService;
import com.wn.pettyloan.util.*;
import com.wn.pettyloan.vo.ProductVo;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：Morsera
 * @date ：Created in 2022/12/7 18:28
 * @description：产品表
 */
@RestController
@RequestMapping("products")
public class ProductController {
    @Resource
    private OssUtil ossUtil;
    @Resource
    private ProductService productService;
    /**
     * @Description: 上传产品封面
     * @param file
     * @return: com.wn.pettyloan.util.Result
     * @Author: Morsera
     * @Date: 2022/12/7 18:33
     */
    @PostMapping("/uploadProductCover")
    public Result fileUpload(@RequestParam("file") MultipartFile file) {
        try {
            String url = ossUtil.uploadFile(file); //调用OSS工具类
            return Result.ok(url);
        } catch (Exception e) {
            return Result.error("上传失败");
        }
    }

    /*
      * @Author 国雨浓
      * @Description  添加产品 判断产品名字不能重复
      * @Date 12:11 2022/12/8
      * @Param
      * @return
      **/
    @PostMapping("/add")
    public Result add(@RequestBody Product product){
        //查询产品重名
        Product product1 = productService.ProductFound(product.getProductName());
        if (product1 == null) {
            productService.insertSelective(product);
            return Result.ok();
        }else {
            return Result.error();
        }
    }

    /*
      * @Author 国雨浓
      * @Description  根据id查询产品
      * @Date 12:06 2022/12/8
      * @Param
      * @return
      **/
    @GetMapping("{id}")
    public Result queryById(@PathVariable Long id){
        Product product = productService.selectByPrimaryKey(id);
        return Result.ok().data("product",product);
    }
    /*
      * @Author 国雨浓
      * @Description  查询全部产品
      * @Date 14:55 2022/12/8
      * @Param
      * @return
      **/
    @GetMapping("/selectAll")
    public Result selectAll(){
        List<Product> products = productService.selectAll();
        return Result.ok().data("product",products);
    }

    /*
      * @Author 国雨浓
      * @Description  修改产品
      * @Date 12:08 2022/12/8
      * @Param
      * @return
      **/
    @PutMapping("")
    public Result update(@RequestBody Product product ){
        productService.updateByPrimaryKeySelective(product);
        return Result.ok();
    }

    /*
      * @Author 国雨浓
      * @Description 根据ID删除产品
      * @Date 12:08 2022/12/8
      * @Param
      * @return
      **/
    @DeleteMapping("{id}")
    public Result delete(@PathVariable Long id){
        productService.deleteByPrimaryKey(id);
        return Result.ok();
    }

    /*
      * @Author 国雨浓
      * @Description  产品模糊查询
      * @Date 16:14 2022/12/8
      * @Param
      * @return
      **/
    @PostMapping("/query")
    public Result query(@RequestBody ProductDto productDto) {
        //添加排序功能
        List<Product> products = productService.selectByLike(productDto);
        return Result.ok().data("product",products);
    }

    
    /*
      * @Author 国雨浓
      * @Description  修改产品审核状态
      * @Date 10:43 2022/12/12
      * @Param 
      * @return 
      **/
    @PutMapping("/audit")
    public Result audit(@RequestBody Product product ){
        Integer productAudit = product.getProductAudit();
        if (productAudit.equals(1)){
            product.setProductAudit(2);
            int audit = productService.audit(product);
            return Result.ok();
        }else {
            return Result.error("");
        }

    }

    @PutMapping("/audit2")
    public Result audit2(@RequestBody Product product ){
        Integer productAudit = product.getProductAudit();
        if (productAudit.equals(1)){
            product.setProductAudit(3);
            int audit = productService.audit(product);
            return Result.ok();
        }else {
            return Result.error("");
        }

    }
    /*
      * @Author 国雨浓
      * @Description  根据产品名字查id
      * @Date 11:33 2022/12/12
      * @Param 
      * @return 
      **/
    @GetMapping("/selByName/{name}")
    public Result selectByName(@PathVariable("name") String name){
        Product product = productService.selectByName(name);
        return Result.ok().data("product",product);
    }

    /**
     * @param productDto
     * @Description: 产品信息导出
     * @return: org.springframework.web.servlet.ModelAndView
     * @Author: Morsera
     * @Date: 2022/12/16 17:59
     */
    @RequestMapping(value = "/exportExcel")
    public ModelAndView exportExcel(@RequestBody ProductDto productDto) {
        ModelAndView mv = new ModelAndView();
        try {
            Map<String, Object> dataMap = new HashMap<>();
            //构造列名称
            List<String> titles = new ArrayList<>();
            titles.add("产品编号"); // 1
            titles.add("产品名称"); // 2
            titles.add("产品类型"); // 3
            titles.add("产品利率"); // 4
            titles.add("贷款额度"); // 5
            titles.add("贷款期限"); // 6
            dataMap.put("titles", titles);
            //得到被导出的数据
            List<ProductVo> varOList = productService.exportExcel(productDto);
            List<PageData> varList = new ArrayList<>();
            for (ProductVo productVo : varOList) {
                PageData vpd = new PageData();
                vpd.put("var1", productVo.getProductId());
                vpd.put("var2", productVo.getProductName());
                vpd.put("var3", productVo.getProductType());
                vpd.put("var4", productVo.getProductRate());
                vpd.put("var5", productVo.getProductLoanAmount());
                vpd.put("var6", productVo.getLoanTime());
                varList.add(vpd);
            }
            dataMap.put("varList", varList);
            ObjectExcelView erv = new ObjectExcelView();
            mv = new ModelAndView(erv, dataMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mv;
    }

    /**
     * @Description: 数据导入
     * @param file
     * @param request
     * @return: com.wn.pettyloan.util.Result
     * @Author: Morsera
     * @Date: 2022/12/16 16:38
     */
    @RequestMapping(value = "/importExcel")
    public Result importExcel(MultipartFile file, HttpServletRequest request) throws Exception {

        if (null != file && !file.isEmpty()) {
            //执行读EXCEL操作,读出的数据导入List 2:从第2行开始；0:从第A列开始；0:第0个sheet
            List<PageData> listPd = (List) ObjectExcelRead.readExcel(file.getInputStream(), 1, 0, 0);
            for (PageData pageData : listPd) {
                Product product = new Product();
                String strId = pageData.getString("var0");
                int id = 0;
                if (strId.indexOf(".") != -1) {
                    id = Integer.parseInt(strId.substring(0, strId.indexOf(".")));
                } else {
                    id = Integer.parseInt(strId);
                }
                product.setProductId(id);
                product.setProductName(pageData.getString("var1"));
                productService.insertSelective(product);
            }

        }
        return Result.ok("导入成功");
    }

}
